package net.lab1024.sa.user.module.business.course.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.common.common.domain.PageResult;
import net.lab1024.sa.common.common.util.SmartPageUtil;
import net.lab1024.sa.user.module.business.course.dao.CourseDao;
import net.lab1024.sa.user.module.business.course.domain.form.CourseQueryForm;
import net.lab1024.sa.user.module.business.course.domain.form.CourseTeacherQueryForm;
import net.lab1024.sa.user.module.business.course.domain.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 课程表 Service
 *
 * @Author jabari
 * @Date 2023-08-05 16:37:20
 * @Copyright 51dev.cn
 */

@Service
public class CourseService {

    @Autowired
    private CourseDao courseDao;
    @Autowired
    private PartnerService partnerService;

    @Autowired
    private CourseTeacherService courseTeacherService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CoursePeriodService coursePeriodService;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<CourseVO> queryPage(CourseQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<CourseVO> list = courseDao.queryPage(page, queryForm);

        PageResult<CourseVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        if (pageResult.getEmptyFlag()) {
            return pageResult;
        }
        

        //获取合作方ID集合
        List<Integer> partnerIdList = list.stream().map(CourseVO::getPartnerId).distinct().collect(Collectors.toList());

        //根据合作方ID集合批量获取合作方实体MAP
        Map<Integer, PartnerVO> partners = partnerService.getPartners(partnerIdList);

        //获取课程ID集合
        List<Integer> courseIdList=list.stream().map(CourseVO::getCourseId).distinct().collect(Collectors.toList());

        //根据课程ID集合批量获取教练ID集合
        Map<Integer,List<CourseTeacherVO>> courseTeacherVOMap=new HashMap<>();


        List<CourseTeacherVO> courseTeacherVOList=courseTeacherService.getCourseTeacherVOList(courseIdList);

        //将教练ID集合按课程ID进行分组
        courseTeacherVOList.forEach(courseTeacherVO -> {
            courseTeacherVOMap.computeIfAbsent(courseTeacherVO.getCourseId(),k->new ArrayList<>()).add(courseTeacherVO);
        });

        //根据教练ID集合批量获取教练实体MAP

        List<Integer>  teacherIdList=courseTeacherVOList.stream().map(CourseTeacherVO::getTeacherId).collect(Collectors.toList());

        Map<Integer, TeacherVO> teacherVOMap=teacherService.getTeachers(teacherIdList);

        //根据课程ID集合批量获取上课时段集合
        List<CoursePeriodVO> coursePeriodVOList = coursePeriodService.getCoursePeriodVOList(courseIdList);

        //对上课时段集合按课程ID分组
        Map<Integer,List<CoursePeriodVO>> coursePeriodVOMap=new HashMap<>();
        coursePeriodVOList.forEach(coursePeriodVO -> {
            coursePeriodVOMap.computeIfAbsent(coursePeriodVO.getCourseId(),k->new ArrayList<>()).add(coursePeriodVO);
        });

        list.forEach(e->{

            if(!partners.isEmpty()){

                //设置合作方
                e.setPartnerName(partners.get(e.getPartnerId()).getPartnerName());
            }


            if(!courseTeacherVOMap.isEmpty()){
                //设置教练
                e.setTeacherList(courseTeacherVOMap.get(e.getCourseId()).stream().map(courseTeacherVO -> {
                    TeacherVO teacherVO=teacherVOMap.get(courseTeacherVO.getTeacherId());
                    return teacherVO;
                }).collect(Collectors.toList()));
            }

            if(!coursePeriodVOMap.isEmpty()){
                Map<String,List<CoursePeriodVO>> coursePeriodVOListMap=new HashMap<>();

                coursePeriodVOMap.get(e.getCourseId()).forEach(coursePeriodVO -> {
                    coursePeriodVOListMap.computeIfAbsent(coursePeriodVO.getCourseDate().format(DateTimeFormatter.ISO_LOCAL_DATE), k->new ArrayList<>()).add(coursePeriodVO);
                });

                e.setPeriodList(coursePeriodVOListMap);

            }




        });

        return pageResult;
    }


}
